//
//  String+Category.swift
//  HuaYun
//
//  Created by enghou on 2018/6/28.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import UIKit
extension String{
    
    var isNumberic: Bool {
        get{
            let reg = "^[0-9]+(\\.?[0-9]{1,2})"
            let predicate = NSPredicate.init(format: "SELF MATCHES %@", reg)
            return predicate.evaluate(with: self)
        }
    }
    
    var ifrmbformat: Bool {
        get{
            let strarr = self.split(separator: Character.init(","))
            if strarr.count == 0 {
                return false
            }
            let result = strarr.reduce(true) { (res, substr) -> Bool in
                let reg = "[0-9]+(\\.[0-9]{1,2})"
                let predicate = NSPredicate.init(format: "SELF MATCHES %@", reg)
                return res && predicate.evaluate(with: self)
            }
            
            return result
        }
    }
    
    //100,100,000
    var rmbformat: String {
        get{

            if self.isNumberic == true {
                let value = Double.init(self)!
                return value.currencyStr.trimmingCharacters(in: CharacterSet.init(charactersIn: "CN¥")).replacingOccurrences(of: ".00", with: "")
            }
            return self
        }
    }
    
    
    
    
    //将数字转换为人民币大写
    var rmbOldStyle: String {
        get {
            guard let num = Double(self) else {
                return ""
            }
            let format = NumberFormatter()
            format.locale = Locale(identifier: "zh_CN")
            format.numberStyle = .spellOut
            format.minimumIntegerDigits = 1
            format.minimumFractionDigits = 0
            format.maximumFractionDigits = 2
            let text = format.string(from: NSNumber(value: num)) ?? ""
            let sept = self.components(separatedBy: ".")
            let decimals: Double? = sept.count == 2 ? Double("0." + sept.last!) : nil
            return self.formatRMM(text: text, isInt: decimals == nil || decimals! < 0.01)
        }
    }
    
    
    private func formatRMM(text: String, isInt: Bool) -> String {
        let formattedString = text.replacingOccurrences(of: "一", with: "壹")
            .replacingOccurrences(of: "二", with: "贰")
            .replacingOccurrences(of: "三", with: "叁")
            .replacingOccurrences(of: "四", with: "肆")
            .replacingOccurrences(of: "五", with: "伍")
            .replacingOccurrences(of: "六", with: "陆")
            .replacingOccurrences(of: "七", with: "柒")
            .replacingOccurrences(of: "八", with: "捌")
            .replacingOccurrences(of: "九", with: "玖")
            .replacingOccurrences(of: "十", with: "拾")
            .replacingOccurrences(of: "百", with: "佰")
            .replacingOccurrences(of: "千", with: "仟")
            .replacingOccurrences(of: "〇", with: "零")
        let sept = formattedString.components(separatedBy: "点")
        var intStr = sept[0]
        if sept.count > 0 && isInt {
            // 整数处理
            return intStr.appending("元整")
        } else {
            // 小数处理
            let decStr = sept[1]
            intStr = intStr.appending("元").appending("\(decStr.first!)角")
            if decStr.count > 1 {
                intStr = intStr.appending("\(decStr[decStr.index(decStr.startIndex, offsetBy: 1)])分")
            } else {
                intStr = intStr.appending("零分")
            }
            return intStr
        }
    }
    
    var md5: String{
        get{
            let cStr = self.cString(using: .utf8)
            let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
            CC_MD5(cStr!,(CC_LONG)(strlen(cStr!)), buffer)
            let md5String = NSMutableString();
            for i in 0 ..< 16{
                md5String.appendFormat("%02x", buffer[i])
            }
            free(buffer)
            return md5String as String
        }
    }
    
    
    
    
    func size(with font: UIFont) -> CGSize {
        return self.size(with: font, maxSize: CGSize(width: 1000.0, height: 1000.0))
    }
    
    func size(with font: UIFont,maxSize: CGSize) -> CGSize {
        
        let normalText: NSString = self as NSString
//
        let dic = [NSAttributedString.Key.font : font]
//
        let stringSize = normalText.boundingRect(with: maxSize, options: .usesLineFragmentOrigin, attributes: dic, context:nil).size
//
        return stringSize
        
    }
    
}

extension String{
    var isPhone: Bool{
        get{
           
            let pattern = "^1[0-9]{10}$"
            
            do{
                
                let regex = try NSRegularExpression.init(pattern: pattern, options: .caseInsensitive)
                
                let matches = regex.matches(in: self, options: .reportProgress, range: NSRange(location: 0, length: self.count))
                
                return matches.count > 0
                
            } catch {
                return false
            }
        }
    }
    
}

